/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.poemPoet.rest;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.poemPoet.domain.PoemPoet;
import me.zhengjie.poemPoet.domain.vo.domain.vo.PoemPoetQueryCriteria;
import me.zhengjie.poemPoet.service.PoemPoetService;
import me.zhengjie.utils.PageResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
* @author hwl
* @date 2024-12-08
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "poemPoet管理")
@RequestMapping("/api/poemPoet")
public class PoemPoetController {

    private final PoemPoetService poemPoetService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @AnonymousAccess
    public void exportPoemPoet(HttpServletResponse response, PoemPoetQueryCriteria criteria) throws IOException {
        poemPoetService.download(poemPoetService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询poemPoet")
    @ApiOperation("查询poemPoet")
    @AnonymousAccess
    public ResponseEntity<PageResult<PoemPoet>> queryPoemPoet(PoemPoetQueryCriteria criteria,
                                                              Page<Object> page){
        return new ResponseEntity<>(poemPoetService.queryAll(criteria,page),HttpStatus.OK);
    }

    @GetMapping(value="/paged")
    @Log("分页查询poemPoet")
    @ApiOperation("分页查询poemPoet")
    @AnonymousAccess
    public ResponseEntity<PageResult<PoemPoet>> pagedQueryPoemPoet(PoemPoetQueryCriteria criteria,
                                                              @RequestParam(defaultValue = "1") int current,
                                                              @RequestParam(defaultValue = "20") int size  ){
        Page<Object> page = new Page<>(current,size);
        return new ResponseEntity<>(poemPoetService.queryAll(criteria,page),HttpStatus.OK);
    }


    @PostMapping
    @Log("新增poemPoet")
    @ApiOperation("新增poemPoet")
    @AnonymousAccess
    public ResponseEntity<Object> createPoemPoet(@Validated @RequestBody PoemPoet resources){
        poemPoetService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改poemPoet")
    @ApiOperation("修改poemPoet")
    @AnonymousAccess
    public ResponseEntity<Object> updatePoemPoet(@Validated @RequestBody PoemPoet resources){
        poemPoetService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除poemPoet")
    @ApiOperation("删除poemPoet")
    @AnonymousAccess
    public ResponseEntity<Object> deletePoemPoet(@RequestBody List<Integer> ids) {
        poemPoetService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/all")
    @Log("查询所有poem")
    @ApiOperation("查询所有poem")
    @AnonymousAccess
    public List<PoemPoet> getALL(){
        return poemPoetService.getALl();
    }

    @GetMapping("/getById")
    @Log("通过主键查询PoemPoet")
    @ApiOperation("通过主键的PoemPoet")
    @AnonymousAccess
    public ResponseEntity<PoemPoet> getPoemPoetById(@RequestParam Integer poemId){
        // 调用服务层方法获取实体对象
        PoemPoet poemPoet = poemPoetService.getPoemPoetById(poemId);

        // 判断结果是否存在，返回对应状态码
        if (poemPoet != null) {
            return ResponseEntity.ok(poemPoet); // 200 OK + 实体数据
        } else {
            return ResponseEntity.notFound().build(); // 404 Not Found
        }
    }

    @GetMapping("/favorite")
    @Log("查询我收藏的PoemPoet")
    @ApiOperation("查询我收藏的PoemPoet")
    @AnonymousAccess
    public List<PoemPoet> queryMyFavoritePoem(@RequestParam String userName){
        return poemPoetService.getMyFavoritePoem(userName);
    }
}